package com.example.gulimall.ssoserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
//import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: 平淡还心动
 * @createTime: 2020-06-29 19:56
 **/

@Controller
public class LoginController {

    @Autowired
    StringRedisTemplate redisTemplate;


    @ResponseBody
    @GetMapping("/userinfo")
    public String userinfo(@RequestParam(value = "token") String token) {
        String s = redisTemplate.opsForValue().get(token);

        return s;

    }


@GetMapping("/login.html")
public String loginPage(@RequestParam("redirect_url") String url,Model model,
                       @CookieValue(value = "sso_token",required = false)  String sso_token) {
    if (!StringUtils.isEmpty(sso_token)){
        // 不为空，说明之前有人登录过了。浏览器留下了痕迹
        return "redirect:" + url + "?token=" + sso_token;
    }
    model.addAttribute("url",url);
    return "login";
}

    /**
     * 使用cookie标记用户登陆过，使用sso单点登录
     * @param username
     * @param password
     * @param url
     * @param response
     * @return
     */
    @PostMapping(value = "/doLogin")
    public String doLogin(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          @RequestParam("redirect_url") String url,
                          HttpServletResponse response) {

        //登录成功跳转，跳回到登录页
        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {

            String uuid = UUID.randomUUID().toString().replace("_", "");

            Cookie sso_token = new Cookie("sso_token", uuid);

            response.addCookie(sso_token);
            //登录成功的用户存起来
            redisTemplate.opsForValue().set(uuid,username);
            return "redirect:" + url + "?token=" + uuid;
        }
        return "login";
    }

//    @PostMapping(value = "/doLogin")
//    public String doLogin(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("redirect_url") String url, HttpServletResponse response) {
//
//        //登录成功跳转，跳回到登录页
//        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
//
//            String uuid = UUID.randomUUID().toString().replace("_", "");
//           // redisTemplate.opsForValue().set(uuid, username);
//            Cookie sso_token = new Cookie("sso_token", uuid);
//
//            response.addCookie(sso_token);
//            return "redirect:" + url + "?token=" + uuid;
//        }
//        return "login";
//    }

}
